<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 托管支付类
 * $content 格式：{DATE=Y-m-d h:m:s|METHOD=[method名]|RESULT={ParamName1#ParamNamevalue1#,ParamName2#ParamNamevalue2#,...}}\n\r
 * 本系统定义  "000000" 指未知错误
 */
class Trusteeship_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
        $this->load->library ( 'MyAES' );
        //$this->MyAES->encrypt ( $data,$key,$iv ); 加密
        //$this->MyAES->decrypt ( $sData,$sKey,$iv ); 解密
    }
    
    /**
     * 绑定账号
     * params:
     */
    function binding_account($params){
    	$url = "https://paytest.baofoo.com/baofoo-custody/custody/quickRegister.do";
    	$info = "";
    	if(!isset($params['has_bf_account'])){
    		$info = "是否已有宝付会员账号不允许为空";   //（0 无 、 1有）
    	}else if(!isset($params['bf_account'])){
    		$info = "宝付会员账号不允许为空";
    	}else if(!isset($params['user_id'])){
    		$info = "用户编号不允许为空";
    	}else if(!isset($params['real_name'])){
    		$info = "用户真实姓名不允许为空";
    	}else if(!isset($params['id_card'])){
    		$info = "用户真实身份证号码不允许为空";
    	}else if(!isset($params['bind_code'])){
    		$info = "平台绑定码不允许为空";
    	}
    	if($info == ""){
    		// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
    		// 请求报文体
	    	$requestParams = "<custody_req>
    								<has_bf_account>".$params['has_bf_account']."</has_bf_account>
    								<bf_account>".$params['bf_account']."</bf_account>
    								<user_id>".$params['user_id']."</user_id>
    								<real_name>".$params['real_name']."</real_name>
    								<id_card>".$params['id_card']."</id_card>
    								<bind_code>".$params['bind_code']."</bind_code>
    								<account_type>1</account_type>
    							</custody_req>";
	    	$this->load->library ('MyAES');
	    	$requestParams = $this->myaes->encryptString($XmlHead.$requestParams,KEY,IV); //加密
			// 报文参数
	 		$post_data = self::getPostdata($requestParams);
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"binding_account");
    	}
    	return  $info;
    }
    
    /**
     * 绑定验证
     */
    function binding_validation($params){
    	$url = " https://paytest.baofoo.com/baofoo-custody/custody/sendBindCode.do ";
    	$info = "";
    	if(!$params['bf_account']){
    		$info = "宝付会员账号不允许为空";
    	}
    	if($info == ""){
	    	$requestParams =$params['bf_account'];
			// 报文参数
		 	$post_data = self::getPostdata($requestParams);
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"binding_validation");
    	}
    	return  $info;
    }
    
	/**
     * 授权协议（页面接口）
     */
    function agreement($params){
    	$url = "http://paytest.baofoo.com/baofoo-custody/custody/inAccredit.do";
    	$info = "";
    	if(!$params['user_id']){
    		$info = "会员账号不允许为空";
    	}
    	else if(!$params['service_url']){
    		$info = "服务端通知地址不允许为空";
    	}
   		else if(!$params['page_url']){
    		$params['page_url'] = "";
    	}
    	if($info == ""){
	    	$post_data = array("merchant_id"=>MERCHANT_ID,"terminal_id"=>TERMINAL_ID,
	    					   "user_id"=>$params['user_id'],"service_url"=>$params['service_url'],"page_url"=>$params['page_url']
	    						);
	 
	    	 // 发起请求
		 	$info = self::httpRequestPOST($url,$post_data);//请求接口
    	}
    	return  $info;//返回页面脚本
    }
    
	/**
     * 余额查询
     */
    function balances($params){
    	$url = "https://paytest.baofoo.com/baofoo-custody/custody/accountBalance.do";
    	$info = "";
    	if(!$params['user_id']){
    		$info = "会员不允许为空";
    	}
    	if($info == ""){
    		// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
	    	$requestParams ="<custody_req>
	    						<merchant_id>".MERCHANT_ID."</merchant_id>
	    						<user_id>".$params['user_id']."</user_id>
	    					</custody_req>";
	 		// 报文参数
	 		$post_data = self::getPostdata($XmlHead.$requestParams);
	    	// 发起请求
		 	$result = self::httpRequestPOST($url,$post_data);//请求接口
		 	// 响应报文解析
		 	$xml = simplexml_load_string($result);//结果解析
		    // 响应报文验签
		    $sign = $xml->sign;
		    $local_sign = Md5($xml->code."~|~".$xml->balance."~|~".KEY);
		    $content = "{DATE=".date('Y-m-d h:m:s',time())."|METHOD=balances|RESULT={code#".$xml->code."#,balance#".$xml->balance."#}}\r\n";
		    file_put_contents(TRUSTEESHIP_LOG, $content,FILE_APPEND);
		    if($sign == $local_sign){// 验签成功
		    	$info = $xml->balance; //返回查询的金额，-1说明出异常或者没有授权
		    }else { // 未知错误
		    	$info = "000000";
		    }
	    }
    	return  $info;
    }
    
	/**
     * 商户会员余额总值
     */
    function surplus(){
    	$url = "https://paytest.baofoo.com/baofoo-custody/custody/accountAllBalance.do";
    	$info = "";
    	if($info == ""){
	    	$requestParams = date('Y-m-d',time());
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($requestParams);
    		// 发起请求
		 	$result = self::httpRequestPOST($url,$post_data);//请求接口
		 	// 响应报文解析
		 	$xml = simplexml_load_string($result);//结果解析
		    // 响应报文验签
		    $sign = $xml->sign;
		    $local_sign = Md5($xml->code."~|~".$xml->balance."~|~".KEY);
		    $content = "{DATE=".date('Y-m-d h:m:s',time())."|METHOD=surplus|RESULT={code#".$xml->code."#,balance#".$xml->balance."#}}\r\n";
		    file_put_contents(TRUSTEESHIP_LOG, $content,FILE_APPEND);
		    if($sign == $local_sign){// 验签成功
		    	$info = $xml->balance; //返回查询的金额，-1说明出异常或者没有授权
		    }else { // 未知错误
		    	$info = "000000";
		    }
    	}
    	return  $info;
    }
    
    
	/**
     * 充值
     */
    function recharge($params){
    	$url = "https://paytest.baofoo.com/baofoo-custody/custody/recharge.do";
    	$info = "";
    	if(!$params['user_id']){
    		$info = "用户编号不允许为空";
    	}else if(!$params['order_id']){
    		$info = "订单号不允许为空";
    	}else if(!$params['amount']){
    		$info = "金额不允许为空";
    	}else if(!$params['fee']){
    		$params['fee'] = "0.0";
    	}else if(!$params['fee_taken_on']){
    		$info = "费用承担方(1或2)不允许为空"; //1平台2用户自担
    	}else if(!$params['additional_info']){
    		$params['additional_info'] = "";
    	}else if(!$params['page_url']){
    		$info = "充值页面资源请求不允许为空";
    	}else if(!$params['return_url']){
    		$info = "充值后台资源请求不允许为空";
    	}
    	if($info == ""){
    		// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
    		// 请求报文体
	    	$requestParams = "<custody_req>
    								<merchant_id>".MERCHANT_ID."</merchant_id>
									<user_id>".$params['user_id']."</user_id>
									<order_id>".$params['order_id']."</order_id>
									<amount>".$params['amount']."</amount>
									<fee>".$params['fee']."</fee>
									<fee_taken_on>".$params['fee_taken_on']."</fee_taken_on>
									<additional_info>".$params['additional_info']."</additional_info>
									<page_url>".$params['page_url']."</page_url>
									<return_url>".$params['return_url']."</return_url>
    							</custody_req>";
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($XmlHead.$requestParams);
	 		
	    	// 请求并获取相应结果
	    	$info = self::httpRequestPOST($url,$post_data);//请求接口
    	}
    	return  $info;
    }
    
	/**
     * 提现(服务端接口)
     */
    function mention_now($params){
    	$url = "https://paytest.baofoo.com/baofoo-custody/custody/foChargeAPI.do";
    	$info = "";
    	if(!$params['order_id']){
    		$info = "提现订单号不允许为空";
    	}else if(!$params['user_id']){
    		$info = "用户编号不允许为空";
    	}else if(!$params['amount']){
    		$info = "金额不允许为空";
    	}else if(!$params['fee']){
    		$params['fee'] = "0.0";
    	}else if(!$params['fee_taken_on']){
    		$info = "宝付手续费，收取方(1或2)不允许为空"; //1平台2个人
    	}else if(!$params['bank_no']){
    		$info = "银行卡号不允许为空";
    	}
    	if($info == ""){
    		// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
    		// 请求报文体
	    	$requestParams = "<custody_req>
									<user_id>".$params['user_id']."</user_id>
									<order_id>".$params['order_id']."</order_id>
									<fee>".$params['fee']."</fee>
									<fee_taken_on>".$params['fee_taken_on']."</fee_taken_on>
									<amount>".$params['amount']."</amount>
									<bank_no>".$params['bank_no']."</bank_no>
    							</custody_req>";
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($XmlHead.$requestParams);
	 		
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"mention_now");
    	}
    	return  $info;
    }
    
    
	/**
     * 转账(服务端接口)
     */
    function transfer_accounts($params){
    	$url = "https://paytest.baofoo.com/baofoo-custody/custody/acctTrans.do";
    	$info = "";
    	if(!$params['order_id']){
    		$info = "提现订单号不允许为空";
    	}else if(!$params['payer_user_id']){
    		$info = "付款方用户编号不允许为空";
    	}else if(!$params['payee_user_id']){
    		$info = "收款方用户编号不允许为空";
    	}else if(!isset($params['payer_type'])){
    		$info = "付款方帐号类型(0或1)不允许为空"; //0为普通用户(平台的user_id) 1为商户号
    	}else if(!isset($params['payee_type'])){	
    		$info = "收款方帐号类型(0或1)不允许为空";	//0为普通用户(平台的user_id) 1为商户号
    	}else if(!$params['amount']){
    		$info = "金额不允许为空";
    	}else if(!$params['fee']){
    		$params['fee'] = "0.0";
    	}else if(!isset($params['fee_taken_on'])){
    		$info = "宝付手续费，收取方(0或1)不允许为空";
    	}
    	if($info == ""){
    		// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
    		// 请求报文体
	    	$requestParams = "<custody_req>
    								<merchant_id>".MERCHANT_ID."</merchant_id>
									<order_id>".$params['order_id']."</order_id>
									<payer_user_id>".$params['payer_user_id']."</payer_user_id>
									<payee_user_id>".$params['payee_user_id']."</payee_user_id>
									<payer_type>".$params['payer_type']."</payer_type>
									<payee_type>".$params['payee_type']."</payee_type>
									<amount>".$params['amount']."</amount>
									<fee>".$params['fee']."</fee>
									<fee_taken_on>".$params['fee_taken_on']."</fee_taken_on>
									<req_time>".(time()*1000)."</req_time>
    							</custody_req>";
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($XmlHead.$requestParams);
	 		
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"transfer_accounts");
    	}
    	return  $info;
    }
    
	/**
     * 投标、流标、满标、还款(服务端接口)
     */
    function business($params){
    	$url = "https://paytest.baofoo.com/baofoo-custody/custody/p2pRequest.do";
    	$info = "";
    	if(!$params['action_type']){
    		$info = "请求类型不允许为空";		//投标为1，满标为2，流标为3，还标为4
    	}else if(!$params['order_id']){
    		$info = "订单号 不允许为空";
    	}else if(!$params['cus_id']){
    		$info = "标id不允许为空";
    	}else if(!$params['cus_name']){
    		$info = "标名称不允许为空";
    	}else if(!$params['brw_id']){
    		$info = "借款人用户编号不允许为空";
    	}else if(!$params['actions']){
    		$info = "内容不允许为空";
    	}else if(!$params['fee']){
    		$params['fee'] = "0.0";
    	}
    	if($info == ""){
    		$actions = "";
    		$fee = "";
    		if($params['action_type'] == '1'){//投标
    			foreach($params['actions'] as $key=>$value){
    				$actions .= "<action>
									<user_id>".$value['user_id']."</user_id>
									<user_name></user_name>
									<amount>".$value['amount']."</amount>
								</action>";
    			}
    			$fee = "<fee>".$params['fee']."</fee>";
    		}else if($params['action_type'] == '2'){//满标
    			foreach($params['actions'] as $key=>$value){
    				$actions .= "<action>
									<user_id>".$value['user_id']."</user_id>
									<amount>".$value['amount']."</amount>
									<is_voucher>".$value['is_voucher']."</is_voucher>
								</action>";//is_voucher=0表示借款人(唯一)，=1表示担保人
    			}
    			$fee = "<fee>".$params['fee']."</fee>";
    		}else if($params['action_type'] == '3'){//流标
    			foreach($params['actions'] as $key=>$value){
    				$actions .= "<action>
									<user_id>".$value['user_id']."</user_id>
									<amount>".$value['amount']."</amount>
								</action>";
    			}
    			$fee = "<fee>".$params['fee']."</fee>";
    		}else if($params['action_type'] == '4'){//还标
    			foreach($params['actions'] as $key=>$value){
    				$actions .= "<action>
									<user_id>".$value['user_id']."</user_id>
									<amount>".$value['amount']."</amount>
									<fee>".$value['fee']."</fee>
								</action>";
    			}
    			$actions .= "<action>
								<user_id>".$params['brw_id']."</user_id>
								<special>1</special>
								<fee>".$params['fee']."</fee>
							</action>";
    		}
	    	// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
    		// 请求报文体
	    	$requestParams = "<custody_req>
    								<merchant_id>".MERCHANT_ID."</merchant_id>
									<order_id>".$params['order_id']."</order_id>
									<cus_id>".$params['cus_id']."</cus_id>
									<cus_name>".$params['cus_name']."</cus_name>
									<brw_id>".$params['brw_id']."</brw_id>
									<req_time>".(time()*1000)."</req_time>
									<actions>".$actions."</actions>
									".$fee."
    							</custody_req>";
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($XmlHead.$requestParams);
	 
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"business");
    	}
    	return  $info;
    }
    
	/**
     * 业务查询 (服务端接口)
     * order_id 和 查询时间 不能同时为空
     * 单独按时间查询 和单独order_id 查询, 或者同时两种条件查询; type为必填 .
     */
    function query($params){
    	$url = "http://paytest.baofoo.com/baofoo-custody/custody/p2pQuery.do";
    	$info = "";
    	if(!$params['order_id'] && !$params['start_time']){
    		$info = "订单ID与开始时间不允许同时为空";		//订单ID,多ID之间使用英文逗号间隔,单ID不需要逗号
    	}else if(!isset($params['type'])){	//1-投标 2-满标 3-流标 4-还款 5-充值 6-提现 ;
    		$info = "类型不允许为空";
    	}else if($params['start_time'] && !$params['end_time']){
    		$info = "结束时间不允许为空"; 
    	}
    	if($info == ""){
    		// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
    		// 请求报文体
	    	$requestParams = "<custody_req>
    								<order_id>".$params['order_id']."</order_id>
									<type>".$params['type']."</type>
									<start_time>".$params['start_time']."</start_time>
									<end_time>".$params['end_time']."</end_time>
    						 </custody_req>";
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($XmlHead.$requestParams);
	 		
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"query");
    		if($info['res']){
    			if($info['result']){
		    		$xml = $info['result'];
		    		$i = 0;
		    	 	foreach($xml->children() as $child){  //获取XML对象里面的每一个子节点，也是一个类似于数组的对象
		                   $info['msg'.$i ] = array('order_id'=>$child->order_id,'state'=>$child->state,'succ_amount'=>$child->succ_amount,
		                   				'succ_time'=>$child->succ_time,'fee'=>$child->fee,'baofoo_fee'=>$child->baofoo_fee,'fee_taken_on'=>$child->fee_taken_on);
		            	$i++;
		    	 	}
    			}
    			unset($info['result']);
	    	}
    	}
    	return  $info;
    }
    
	/**
     * 银行卡绑定、删除接口
     */
    function binding_bank($params){
    	$url = "http://paytest.baofoo.com/baofoo-custody/custody/opBankCard.do";
    	$info = "";
    	if(!$params['user_id']){
    		$info = "用户编号不允许为空";
    	}else if(!isset($params['type'])){	//0删除1新增
    		$info = "类型不允许为空";
    	}else if(!$params['bank_no']){
    		$info = "银行卡号不允许为空";
    	}else if(!$params['pro_value']){
    		$info = "省份不允许为空"; 
    	}else if(!$params['city_value']){	
    		$info = "市区不允许为空";
    	}else if(!$params['bank_name']){
    		$info = "开户行不允许为空";
    	}else if(!$params['bank_address']){
    		$info = "开户支行不允许为空";
    	}else if(!$params['validate_code']){
    		$info = "手机验证码";
    	}
    	if($info == ""){
    		// 请求报文头
    		$XmlHead = "<?xml version='1.0' encoding='UTF-8'?>";
    		// 请求报文体
	    	$requestParams = "<custody_req>
    								<user_id>".$params['user_id']."</user_id>
									<type>".$params['type']."</type>
									<bank_no>".$params['bank_no']."</bank_no>
									<pro_value>".$params['pro_value']."</pro_value>
									<city_value>".$params['city_value']."</city_value>
									<bank_name>".$params['bank_name']."</bank_name>
									<bank_address>".$params['bank_address']."</bank_address>
									<validate_code>".$params['validate_code']."</validate_code>
    							</custody_req>";
	    	$this->load->library ('MyAES');
	    	$requestParams = $this->myaes->encryptString($XmlHead.$requestParams,KEY,IV); //加密
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($requestParams);
	 		
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"binding_bank");
    	}
    	return  $info;
    }

	/**
     *  获取会员银行卡信息
     */
    function bank_info($params){
    	$url = "http://paytest.baofoo.com/baofoo-custody/custody/getBankCardList.do";
    	$info = "";
    	if(!$params['user_id']){
    		$info = "宝付会员账号不允许为空";
    	}
    	if($info == ""){
	    	$requestParams = $params['user_id'];
	    	// 封装请求报文并加密
	    	$post_data = self::getPostdata($requestParams);
	 
	    	// 请求并获取相应结果
	    	$info = self::req7resp($url, $post_data,"bank_info");
	    	if($info['res']){
	    		if($info['info']){
			    	$this->load->library ('MyAES');
					$date = $this->myaes->decryptString($info['info'],KEY,IV); //解密
					$xml = simplexml_load_string($date);//结果解析
					$i = 0;
		    	 	foreach($xml->children() as $child){  //获取XML对象里面的每一个子节点，也是一个类似于数组的对象
		                   $info['bank'.$i ] = array('bank_no'=>$child->bank_no,'bank_name'=>$child->bank_name,
		                   							'pro_value'=>$child->pro_value,'city_value'=>$child->city_value,'bank_address'=>$child->bank_address);
		            	$i++;
		    	 	}
	    		}
	    		unset($info['info']);
	    	}
    	}
    	return  $info;
    }
    
	/**
     *  获取资金托管商户认证书
     */
    function certification(){
    	$url = "http://paytest.baofoo.com/baofoo-custody/custody/credit.do";
    	$post_data = array("merchant_id"=>MERCHANT_ID,"terminal_id"=>TERMINAL_ID);
	 
    	// 请求并获取相应结果
    	$info = self::httpRequestPOST($url,$post_data);//请求接口
    	return $info;
    }
    
 	/**
     *  发送请求，并封装响应结果
     * 
     * */
    private function req7resp($url,$post_data,$method){
   		 // 发起请求
	 	$result = self::httpRequestPOST($url,$post_data);//请求接口
	 	// 响应报文解析
	 	$xml = simplexml_load_string($result);//结果解析
	    // 响应报文验签
	    $sign = $xml->sign;
	    $local_sign = Md5($xml->code."~|~".$xml->msg."~|~".KEY);
	    
	    $content = "{DATE=".date('Y-m-d h:m:s',time())."|METHOD=".$method."|RESULT={code#".$xml->code."#,msg#".$xml->msg."#}}\r\n";
	    file_put_contents(TRUSTEESHIP_LOG, $content,FILE_APPEND);
	    if($sign == $local_sign){// 验签成功
	    	if($xml->code == "CSD000") {//请求成功
	    		$info['res'] = true;
	    		if(isset($xml->info) && $xml->info!=""){
	    			$info['info'] = $xml->info;
	    		}
	    		else if(isset($xml->result) && $xml->result!=""){
	    			$info['result'] = $xml->result;
	    		}
	    	}else {//请求失败
	    		$info = $xml->msg;
	    	}
	    }else { // 未知错误
	    	$info = "000000";
	    }
	    return $info;
    }
    
    /**
     *  获取请求报文(报文加密，报文参数)
     * 
     * */
    private function getPostdata($requestParams){
		// 请求报文体+签名
    	$sign = md5($requestParams."~|~".KEY);
		// 报文参数
	 	$post_data = array("merchant_id"=>MERCHANT_ID,"terminal_id"=>TERMINAL_ID,"sign"=>$sign,"requestParams"=>$requestParams);
	 	return $post_data;
    }
    
    /**
     * PHP请求GET函数
     */
    function httpRequestGET($url){
    	$url2 = parse_url($url);
    	$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
    	$url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);
    	$host_ip = @gethostbyname($url2["host"]);
    	$fsock_timeout=20;
    	if(($fsock = fsockopen($host_ip, 80, $errno, $errstr, $fsock_timeout)) < 0){
    		return false;
    	}

    	$request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : "");
    	$in = "GET " . $request . " HTTP/1.0\r\n";
    	$in .= "Accept: */*\r\n";
    	$in .= "User-Agent: Payb-Agent\r\n";
    	$in .= "Host: " . $url2["host"] . "\r\n";
    	$in .= "Connection: Close\r\n\r\n";
    	if(!@fwrite($fsock, $in, strlen($in))){
    		fclose($fsock);
    		return false;
    	}
    	unset($in);

    	$out = "";
    	while($buff = @fgets($fsock, 2048)){
    		$out .= $buff;
    	}
    	fclose($fsock);
    	$pos = strpos($out, "\r\n\r\n");
    	$head = substr($out, 0, $pos);    //http head
    	$status = substr($head, 0, strpos($head, "\r\n"));    //http status line
    	$body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body
    	if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){
    		if(intval($matches[1]) / 100 == 2){
    			return $body;
    		}else{
    			return false;
    		}
    	}else{
    		return false;
    	}
    }
    
   /**
    * PHP请求POST 函数
    */
    function httpRequestPOST($url,$post_data){
    	$url2 = parse_url($url);
    	$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
    	$url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);
    	$host_ip = @gethostbyname($url2["host"]);
    	$fsock_timeout=20;//秒
    	if(($fsock = fsockopen($host_ip, 80, $errno, $errstr, $fsock_timeout)) < 0){
    		return false;
    	}
    	$request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : "");
    	$needChar = false;
    	foreach($post_data as $key => $val) {
    		$post_data2 .= ($needChar ? "&" : "") . urlencode($key) . "=" . urlencode($val);
    		$needChar = true;
    	}
    	$in = "POST " . $request . " HTTP/1.0\r\n";
    	$in .= "Accept: */*\r\n";
    	$in .= "Host: " . $url2["host"] . "\r\n";
    	$in .= "User-Agent: Lowell-Agent\r\n";
    	$in .= "Content-type: application/x-www-form-urlencoded\r\n";
    	$in .= "Content-Length: " . strlen($post_data2) . "\r\n";
    	$in .= "Connection: Close\r\n\r\n";
    	$in .= $post_data2 . "\r\n\r\n";
    	unset($post_data2);
    	if(!@fwrite($fsock, $in, strlen($in))){
    		fclose($fsock);
    		return false;
    	}
    	unset($in);
    	$out = "";
    	while($buff = fgets($fsock, 2048)){
    		$out .= $buff;
    	}
    	fclose($fsock);
    	$pos = strpos($out, "\r\n\r\n");
    	$head = substr($out, 0, $pos);    //http head
    	$status = substr($head, 0, strpos($head, "\r\n"));    //http status line
    	$body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body
    	if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){
    		if(intval($matches[1]) / 100 == 2){
    			return $body;
    		}else{
    			return false;
    		}
    	}else{
    		return false;
    	}
    }
}